package com.ropen.sso.authorization.osgi;

import java.util.Dictionary;
import java.util.Hashtable;

import org.ops4j.pax.web.service.WebContainer;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.util.tracker.ServiceTracker;

import com.ropen.sso.authorization.constants.AUTH_CONSTANTS;
import com.ropen.sso.authorization.servlets.AccessTokenServlet;
import com.ropen.sso.authorization.servlets.AuthorizationServlet;
import com.ropen.sso.authorization.servlets.EchoServlet;
import com.ropen.sso.authorization.servlets.RequestTokenServlet;

public class Activator implements BundleActivator {
	  private ServiceTracker httpTracker;

	  public void start(BundleContext context) throws Exception {
		
	    httpTracker = new ServiceTracker(context, WebContainer.class.getName(), null) {
	      public void removedService(ServiceReference reference, Object service) {
	        // HTTP service is no longer available, unregister our resources...
	        try {
	        	WebContainer webContainer = (WebContainer)service;
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/authorize");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/request_token");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/access_token");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/echo");
	        	webContainer.unregister("/jsp/*");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/html");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/js");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/css");
	        	webContainer.unregister(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/img");
	        } catch (IllegalArgumentException exception) {
	           // Ignore; servlet registration probably failed earlier on...
	        }
	      }

	      public Object addingService(ServiceReference reference) {
	        // HTTP service is available, register our resources... 
	    	WebContainer webContainer = (WebContainer) this.context.getService(reference);
	        try {
	        	 // create a default context to share between registrations
                final HttpContext httpContext = webContainer.createDefaultHttpContext();
                // register the hello world servlet
                final Dictionary<String, String> initParams = new Hashtable<String, String>();
                initParams.put( "from", "HttpService" ); 
                webContainer.registerServlet(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/authorize", new AuthorizationServlet(), initParams, httpContext);
                webContainer.registerServlet(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/request_token", new RequestTokenServlet(), initParams, httpContext);
                webContainer.registerServlet(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/access_token", new AccessTokenServlet(), initParams, httpContext);
                webContainer.registerServlet(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/echo", new EchoServlet(), initParams, httpContext);
				//register JSP
				webContainer.registerJsps(new String[]{"/jsp/*" }, httpContext);	
                // register html pages as resources
                webContainer.registerResources(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/html", "/html", httpContext);
                webContainer.registerResources(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/js", "/js", httpContext);
                webContainer.registerResources(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/css", "/css", httpContext);
                webContainer.registerResources(AUTH_CONSTANTS.AUTH_WEB_CONTEXTPATH+"/img", "/img", httpContext);
	        } catch (Exception exception) {
	          exception.printStackTrace();
	        }
	        return webContainer;
	      }
	    };
	    // start tracking all HTTP services...
	    httpTracker.open();
		
		
//	    httpTracker = new ServiceTracker(context, HttpService.class.getName(), null) {
//	      public void removedService(ServiceReference reference, Object service) {
//	        // HTTP service is no longer available, unregister our resources...
//	        try {
//	           ((HttpService) service).unregister("/oauth-provider/authorize");
//	           ((HttpService) service).unregister("/oauth-provider/request_token");
//	           ((HttpService) service).unregister("/oauth-provider/access_token");
//	           ((HttpService) service).unregister("/oauth-provider/test");
//	        } catch (IllegalArgumentException exception) {
//	           // Ignore; servlet registration probably failed earlier on...
//	        }
//	      }
//
//	      public Object addingService(ServiceReference reference) {
//	        // HTTP service is available, register our resources...
//	        HttpService httpService = (HttpService) this.context.getService(reference);
//	        try {
//	        	httpService.registerServlet("/oauth-provider/authorize", new AuthorizationServlet(), null, null);
//	        	httpService.registerServlet("/oauth-provider/request_token", new RequestTokenServlet(), null, null);
//	        	httpService.registerServlet("/oauth-provider/access_token", new AccessTokenServlet(), null, null);
//	        	httpService.registerServlet("/oauth-provider/echo", new EchoServlet(), null, null);
//	        	httpService.registerServlet("/oauth-provider/test", new TestServlet(), null, null);
//	        } catch (Exception exception) {
//	          exception.printStackTrace();
//	        }
//	        return httpService;
//	      }
//	    };
	    // start tracking all HTTP services...
//	    httpTracker.open();
	  }

	  public void stop(BundleContext context) throws Exception {
	    // stop tracking all HTTP services...
//	    httpTracker.close();
	  }
}
